﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using MainGameModule.Models;
using MainGameModule.Views;
using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using YourWorld.Common;
using YourWorld.Common.GreenWorldEntitiesService;
using YourWorld.Common.Infrastucture.Events;
using System.Collections.ObjectModel;
using BusinessObjects;

namespace MainGameModule.ViewModels
{
    public class BadgesViewModel : ViewModelBase
    {
        private IRegionManager regionManager;
        private IUnityContainer container;

        private UserBadge selectedBadge;
        private UserBadge wonBadge;
        private ObservableCollection<UserBadge> userBadges;
        private ObservableCollection<UserBadge> badges;
        
        public BadgesViewModel( IRegionManager regionManager, IUnityContainer container )
        {
            this.regionManager = regionManager;
            this.container = container;

            this.InitializeBadges();
            this.CloseBadgesCommand = new DelegateCommand<string>( this.CloseBadgesView );
        }

        public ObservableCollection<UserBadge> UserBadges
        {
            get
            {
                if ( this.userBadges == null )
                    this.userBadges = new ObservableCollection<UserBadge>();

                return this.userBadges;
            }
            set
            {
                if ( this.userBadges == value )
                    return;

                this.userBadges = value;
                this.NotifyPropertyChanged( "UserBadges" );
            }
        }

        public ObservableCollection<UserBadge> Badges
        {
            get
            {
                if ( this.badges == null )
                    this.badges = new ObservableCollection<UserBadge>();

                return this.badges;
            }
            set
            {
                if ( this.badges == value )
                    return;

                this.badges = value;
                this.NotifyPropertyChanged( "Badges" );
            }
        }

        public UserBadge SelectedBadge
        {
            get { return this.selectedBadge; }
            set
            {
                if ( this.selectedBadge != value )
                {
                    this.selectedBadge = value;
                    this.NotifyPropertyChanged( "SelectedBadge" );
                }
            }
        }
        
        public UserBadge WonBadge
        {
            get { return this.wonBadge; }
            set
            {
                if ( this.wonBadge != value )
                {
                    this.wonBadge = value;
                    this.NotifyPropertyChanged( "WonBadge" );
                }
            }
        }

        public ICommand CloseBadgesCommand
        {
            get;
            private set;
        }

        private void InitializeBadges()
        {
            this.UserBadges = new ObservableCollection<UserBadge>();
            GameMainViewModel gameMain = this.container.Resolve<GameMainViewModel>();

            List<int> myBadgesIds = new List<int>();

            if ( String.IsNullOrEmpty( gameMain.Player.Badges ) == false )
            {
                string possessed = gameMain.Player.Badges;

                List<string> stringIds = possessed.Split( ',' ).ToList();

                foreach ( string id in stringIds )
                {
                    int result = -1;
                    if ( Int32.TryParse( id, out result ) )
                    {
                        myBadgesIds.Add( result );
                    }
                }
            }

            foreach ( Badge badge in BadgesRepository.Instance.Badges )
            {
                UserBadge userBadge = new UserBadge()
                {
                    Id = badge.ID,
                    Image = badge.Image,
                    TargetItems = badge.MinimumItems,
                    Description = badge.Description,
                    IsLocked = myBadgesIds.Contains( badge.ID ) == false,
                    TypeId = badge.Type,
                    Title = badge.Title
                };

                this.UserBadges.Add( userBadge );
            }

            this.SelectedBadge = this.UserBadges.First();
        }

        private void CloseBadgesView( string notUsed )
        {
            PlayerStatusManager.Instance.ContinueGame();

            if ( this.WonBadge == null )
            {
                this.regionManager.Regions[RegionNames.GamePopUpRegion].Deactivate( this.container.Resolve<BadgesView>() );
            }
            else
            {
                this.WonBadge = null;
                this.regionManager.Regions[RegionNames.GamePopUpRegion].Deactivate( this.container.Resolve<BadgeCongratsView>() );
            }
        }
    }
}
